﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsContentCollect
    {
        private readonly KeRui.DAL.CmsContentCollect dal = new KeRui.DAL.CmsContentCollect();

        public int Add(KeRui.Model.CmsContentCollect model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsContentCollect> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsContentCollect> list = new List<KeRui.Model.CmsContentCollect>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsContentCollect item = new KeRui.Model.CmsContentCollect();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["ContentID"].ToString() != "")
                    {
                        item.ContentID = int.Parse(dt.Rows[i]["ContentID"].ToString());
                    }
                    item.Title = dt.Rows[i]["Title"].ToString();
                    item.NodeName = dt.Rows[i]["NodeName"].ToString();
                    item.Url = dt.Rows[i]["Url"].ToString();
                    if (dt.Rows[i]["CollectTime"].ToString() != "")
                    {
                        item.CollectTime = DateTime.Parse(dt.Rows[i]["CollectTime"].ToString());
                    }
                    item.State = dt.Rows[i]["State"].ToString();
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public List<KeRui.Model.CmsContentCollect> GetList(string strCondition)
        {
            string strWhere = "";
            string strOrder = "";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsContentCollect", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsContentCollect> GetListByCache(string strCondition)
        {
            string cacheKey = "CmsContentCollectList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsContentCollect>) cache;
        }

        public KeRui.Model.CmsContentCollect GetModel(int ContentID)
        {
            return this.dal.GetModel(ContentID);
        }
    }
}

